package controller

import (
	"github.com/dgrijalva/jwt-go"
	"github.com/labstack/echo/v4"
	"github.com/labstack/gommon/log"
	"law_case_api/dto"
	"law_case_api/models"
	"law_case_api/services"
	"law_case_api/utils"
	"net/http"
	"strconv"
	"time"
)

// CreateContract
// @Summary 创建合同
// @Tags 合同管理
// @Accept plain
// @Produce plain
// @Param CreateContract body dto.ContractInfoReq true " "
// @Success 200 {object} models.BaseResult
// @Failure 400 {object} models.BaseResult
// @Router /wzj/contract/create [post]
func CreateContract(c echo.Context) error {
	resp := models.BaseResult{
		Code: 200,
	}
	user := c.Get("user").(*jwt.Token)
	claims := user.Claims.(*models.JwtCustomClaims)
	userid, _ := strconv.Atoi(claims.UserId)
	if userid == 0 {
		resp.Code = 400
		resp.Message = "用户信息不存在，请重新登录"
		return c.JSON(http.StatusOK, resp)
	}
	contractInfoReq := dto.ContractInfoReq{}
	if err := c.Bind(&contractInfoReq); err != nil {
		resp.Code = 400
		resp.Message = err.Error()
		return c.JSON(http.StatusBadRequest, resp)
	}
	wzjCustomerInfo := models.WzjContractInfo{}
	utils.MapTo(contractInfoReq, &wzjCustomerInfo)

	contract := services.ContractService{}
	if wzjCustomerInfo.Id > 0 {
		wzjCustomerInfo.UpdateUserId = int64(userid)
		boolRes, err := contract.UpdateCustomerContract(wzjCustomerInfo)
		if err == nil && boolRes {
			resp.Message = "更新合同成功"
			return c.JSON(http.StatusOK, resp)
		}
		log.Error("更新合同失败：", err) //增加日志信息
		resp.Code = 400
		resp.Message = "更新合同失败"
		return c.JSON(http.StatusOK, resp)
	} else {
		wzjCustomerInfo.CreateUserId = int64(userid)
		wzjCustomerInfo.CreateDate = time.Now().Unix()
		wzjCustomerInfo.ContractStatus = 2 //默认待审核
		if contractInfoReq.ContractCategory == 4 {
			wzjCustomerInfo.ContractCode = utils.GenerateSpecialCode("HCHT")
		} else {
			wzjCustomerInfo.ContractCode = utils.GenerateSpecialCode("YKHT")
		}
		boolRes, err := contract.CreateCustomerContract(wzjCustomerInfo)
		if err == nil && boolRes {
			resp.Message = "创建合同成功"
			return c.JSON(http.StatusOK, resp)
		}
		log.Error("创建合同失败：", err) //增加日志信息
		resp.Code = 400
		resp.Message = "创建合同失败"
		return c.JSON(http.StatusOK, resp)
	}
}

// ContractList
// @Summary 获取当前登录人可查看的客户列表-用于select
// @Tags 合同管理
// @Accept plain
// @Produce plain
// @Param ContractList body dto.ContractListReq true " "
// @Success 200 {object} models.BaseResult
// @Failure 400 {object} models.BaseResult
// @Router /wzj/contract/list [get]
func ContractList(c echo.Context) error {
	resp := models.BaseResult{
		Code: 200,
	}
	user := c.Get("user").(*jwt.Token)
	claims := user.Claims.(*models.JwtCustomClaims)
	userid, _ := strconv.Atoi(claims.UserId)
	if userid == 0 {
		resp.Code = 400
		resp.Message = "用户信息不存在，请重新登录"
		return c.JSON(http.StatusOK, resp)
	}
	isLaw := false
	LoginType := c.Request().Header["Logintype"]
	if len(LoginType) > 0 {
		if LoginType[0] == "1" {
			isLaw = true
		}
	}
	contractListReq := dto.ContractListReq{}
	if err := c.Bind(&contractListReq); err != nil {
		resp.Code = 400
		resp.Message = err.Error()
		return c.JSON(http.StatusBadRequest, resp)
	}
	contract := services.ContractService{}
	respData, respPageCount, err := contract.GetContractListByCondition(userid, isLaw, contractListReq)
	if err != nil {
		resp.Code = 400
		return c.JSON(http.StatusOK, resp)
	}
	resp.PageCount = respPageCount
	var data []dto.ContractInfoReq
	utils.MapTo(respData, &data)
	resp.Data = data

	return c.JSON(http.StatusOK, resp)
}

// GetContractInfo
// @Summary 根据id获取合同详情
// @Tags 合同管理
// @Accept plain
// @Produce plain
// @Param contractId query string true "合同id"
// @Success 200 {object} models.BaseResult
// @Failure 400 {object} models.BaseResult
// @Router /wzj/contract/get [get]
func GetContractInfo(c echo.Context) error {
	resp := models.BaseResult{
		Code: 200,
	}
	contractId, _ := strconv.Atoi(c.QueryParam("contractId"))
	if contractId == 0 {
		resp.Code = 400
		resp.Message = "合同编号不能为空"
		return c.JSON(http.StatusOK, resp)
	}
	contract := services.ContractService{}

	wzjContractInfo, err := contract.GetContractById(contractId)
	if err != nil {
		resp.Code = 400
		resp.Message = "根据客户id查询客户信息失败"
		return c.JSON(http.StatusOK, resp)
	}
	//格式化
	contractInfoReq := dto.ContractInfoReq{}
	utils.MapTo(wzjContractInfo, &contractInfoReq)

	resp.Data = contractInfoReq
	return c.JSON(http.StatusOK, resp)
}

// @Summary 审核合同状态
// @Tags 合同管理
// @Accept plain
// @Produce plain
// @Param JudgeContract body dto.ContractInfoReq true " "
// @Success 200 {object} models.BaseResult
// @Failure 400 {object} models.BaseResult
// @Router /wzj/contract/update_status [post]
func JudgeContract(c echo.Context) error {
	resp := models.BaseResult{
		Code: 200,
	}
	user := c.Get("user").(*jwt.Token)
	claims := user.Claims.(*models.JwtCustomClaims)
	userid, _ := strconv.Atoi(claims.UserId)
	if userid == 0 {
		resp.Code = 400
		resp.Message = "用户信息不存在，请重新登录"
		return c.JSON(http.StatusOK, resp)
	}
	contractInfoReq := dto.ContractInfoReq{}
	if err := c.Bind(&contractInfoReq); err != nil {
		resp.Code = 400
		resp.Message = err.Error()
		return c.JSON(http.StatusBadRequest, resp)
	}
	wzjCustomerInfo := models.WzjContractInfo{}
	utils.MapTo(contractInfoReq, &wzjCustomerInfo)

	contract := services.ContractService{}
	boolRes, err := contract.JudgeCustomerContractStatus(wzjCustomerInfo)
	if err == nil && boolRes {
		resp.Message = "审核合同成功"
		return c.JSON(http.StatusOK, resp)
	}
	log.Error("审核合同失败：", err) //增加日志信息
	resp.Code = 400
	resp.Message = "审核合同失败"
	return c.JSON(http.StatusOK, resp)
}

// @Summary 获取当前登录人可查看的客户列表-用于select
// @Tags 客户管理
// @Accept plain
// @Produce plain
// @Param contractId query string true "合同id"
// @Success 200 {object} models.BaseResult
// @Failure 400 {object} models.BaseResult
// @Router /wzj/contract/select [get]
func GetCustomerContractSelect(c echo.Context) error {
	resp := models.BaseResult{
		Code: 200,
	}
	customerId, _ := strconv.Atoi(c.QueryParam("customerId"))
	if customerId == 0 {
		resp.Code = 400
		resp.Message = "客户编号不能为空"
		return c.JSON(http.StatusOK, resp)
	}
	contract := services.ContractService{}
	selectValue, err := contract.GetCustomerContractForSelectByCustomer(customerId)
	if err != nil {
		resp.Code = 400
		resp.Message = "根据客户id查询合同信息失败"
		return c.JSON(http.StatusOK, resp)
	}
	resp.Data = selectValue
	return c.JSON(http.StatusOK, resp)
}
